<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>finding_donors_1st | Chen_Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="项目介绍：在这个项目中，你将使用1994年美国人口普查收集的数据，选用几个监督学习算法以准确地建模被调查者的收入。然后，你将根据初步结果从中选择出最佳的候选算法，并进一步优化该算法以最好地建模这些数据。你的目标是建立一个能够准确地预测被调查者年收入是否超过50000美元的模型。这种类型的任务会出现在那些依赖于捐款而存在的非营利性组织。了解人群的收入情况可以帮助一个非营利性的机构更好地了解他们要多大">
<meta property="og:type" content="article">
<meta property="og:title" content="finding_donors_1st">
<meta property="og:url" content="http://yoursite.com/2018/07/10/finding-donors-1st/index.html">
<meta property="og:site_name" content="Chen_Blog">
<meta property="og:description" content="项目介绍：在这个项目中，你将使用1994年美国人口普查收集的数据，选用几个监督学习算法以准确地建模被调查者的收入。然后，你将根据初步结果从中选择出最佳的候选算法，并进一步优化该算法以最好地建模这些数据。你的目标是建立一个能够准确地预测被调查者年收入是否超过50000美元的模型。这种类型的任务会出现在那些依赖于捐款而存在的非营利性组织。了解人群的收入情况可以帮助一个非营利性的机构更好地了解他们要多大">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-07-12T06:29:35.786Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="finding_donors_1st">
<meta name="twitter:description" content="项目介绍：在这个项目中，你将使用1994年美国人口普查收集的数据，选用几个监督学习算法以准确地建模被调查者的收入。然后，你将根据初步结果从中选择出最佳的候选算法，并进一步优化该算法以最好地建模这些数据。你的目标是建立一个能够准确地预测被调查者年收入是否超过50000美元的模型。这种类型的任务会出现在那些依赖于捐款而存在的非营利性组织。了解人群的收入情况可以帮助一个非营利性的机构更好地了解他们要多大">
  
    <link rel="alternate" href="/atom.xml" title="Chen_Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Chen_Blog</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">业精于勤，荒于嬉；行成于思，毁于随。</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-finding-donors-1st" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/07/10/finding-donors-1st/" class="article-date">
  <time datetime="2018-07-10T08:12:48.000Z" itemprop="datePublished">2018-07-10</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      finding_donors_1st
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>项目介绍：<br>在这个项目中，你将使用1994年美国人口普查收集的数据，选用几个监督学习算法以准确地建模被调查者的收入。然后，你将根据初步结果从中选择出最佳的候选算法，并进一步优化该算法以最好地建模这些数据。你的目标是建立一个能够准确地预测被调查者年收入是否超过50000美元的模型。这种类型的任务会出现在那些依赖于捐款而存在的非营利性组织。了解人群的收入情况可以帮助一个非营利性的机构更好地了解他们要多大的捐赠，或是否他们应该接触这些人。虽然我们很难直接从公开的资源中推断出一个人的一般收入阶层，但是我们可以（也正是我们将要做的）从其他的一些公开的可获得的资源中获得一些特征从而推断出该值<br><br>这个项目的数据集来自<a href="https://archive.ics.uci.edu/ml/datasets/Census+Income" target="_blank" rel="noopener">UCI机器学习知识库</a>。这个数据集是由Ron Kohavi和Barry Becker在发表文章”Scaling Up the Accuracy of Naive-Bayes Classifiers: A Decision-Tree Hybrid”之后捐赠的，你可以在Ron Kohavi提供的<a href="https://www.aaai.org/Papers/KDD/1996/KDD96-033.pdf" target="_blank" rel="noopener">在线版本</a>中找到这个文章。我们在这里探索的数据集相比于原有的数据集有一些小小的改变，比如说移除了特征’fnlwgt’ 以及一些遗失的或者是格式不正确的记录。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> scipy <span class="keyword">as</span> sp</span><br><span class="line"><span class="keyword">from</span> sklearn.metrics <span class="keyword">import</span> accuracy_score,precision_score,recall_score,f1_score</span><br><span class="line"><span class="keyword">from</span> sklearn.model_selection <span class="keyword">import</span> train_test_split</span><br><span class="line"><span class="keyword">from</span> sklearn.preprocessing <span class="keyword">import</span> MinMaxScaler</span><br><span class="line"><span class="keyword">from</span> sklearn.linear_model <span class="keyword">import</span> LogisticRegression</span><br><span class="line"><span class="keyword">from</span> sklearn <span class="keyword">import</span> metrics</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> sklearn.externals <span class="keyword">import</span> joblib</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">train_model</span><span class="params">(data)</span>:</span></span><br><span class="line">  x = data[data.columns[:<span class="number">-1</span>]]</span><br><span class="line">  y = data[data.columns[<span class="number">-1</span>]]</span><br><span class="line">  x_train,x_valid，y_train,y_valid = train_test_split(x,y,test_size=<span class="number">0.3</span>,random_state=<span class="number">0</span>)</span><br><span class="line">  model = LogisticRegression(penalty=<span class="string">'l2'</span>,C=<span class="number">10</span>,class_weight=<span class="string">'balanced'</span>)</span><br><span class="line">  model = fit(x,y)</span><br><span class="line">  <span class="comment">#joblib.dump(model,'lr_model.pkl')</span></span><br><span class="line">  y_valid_pred = model.predict(x_valid)</span><br><span class="line">  print(<span class="string">"valid datasets accuracy_score:"</span>,accuracy_score(y_valid,y_valid_pred))</span><br><span class="line">  print(<span class="string">"valid datasets precision_score:"</span>,precision_score(y_valid,y_valid_pred))</span><br><span class="line">  print(<span class="string">"valid datasets recall_score:"</span>,recall_score(y_valid,y_valid_pred))</span><br><span class="line">  print(<span class="string">"F1 values:"</span>,f1_score(y_valid,y_valid_pred))</span><br><span class="line">  y_valid_prob = model.predict_proba(x_valid)</span><br><span class="line">  fpr,tpr,thresholds = metrics.roc_curve(y_valid,y_vy_valid_prob)</span><br><span class="line">  auc = metrics.auc(fpr,tpr)</span><br><span class="line">  print(<span class="string">"auc"</span>,auc)</span><br><span class="line">  <span class="comment"># 输出ROC曲线</span></span><br><span class="line">  plt.figure(facecolor=<span class="string">'w'</span>)</span><br><span class="line">  plt.plot(fpr,tpr,c=<span class="string">'r'</span>,lw=<span class="number">2</span>,alpha=<span class="number">0.9</span>,label=<span class="string">'AUC%.3f'</span> % auc)</span><br><span class="line">  plt.plot((<span class="number">0</span>,<span class="number">1</span>),(<span class="number">0</span>,<span class="number">1</span>),c=‘b’,lw=<span class="number">1.5</span>,ls=<span class="string">'--'</span>)</span><br><span class="line">  plt.xlim((<span class="number">-0.01</span>,<span class="number">1.02</span>))</span><br><span class="line">  plt.ylim((<span class="number">-0.01</span>,<span class="number">1.02</span>))</span><br><span class="line">  plt.xticks(np.arange(<span class="number">0</span>,<span class="number">1.1</span>,<span class="number">0.1</span>))</span><br><span class="line">  plt.yticks(np.arange(<span class="number">0</span>,<span class="number">1.1</span>,<span class="number">0.1</span>))</span><br><span class="line">  plt.xlabel(<span class="string">'False Positive Rate'</span>,fontsize=<span class="number">14</span>)</span><br><span class="line">  plt.ylabel(<span class="string">'True Positive Rate'</span>,fontsize=<span class="number">14</span>)</span><br><span class="line">  plt.grid(<span class="keyword">True</span>)</span><br><span class="line">  plt.legend(loc=<span class="string">'lower right'</span>,fontsize=<span class="number">14</span>)</span><br><span class="line">  plt.title(<span class="string">"finding_donors ROC and AUC value"</span>,fontsize=<span class="number">17</span>)</span><br><span class="line">  plt.show()</span><br><span class="line">  <span class="keyword">return</span> model</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">  raw_data = pd.read_csv(<span class="string">"train.csv"</span>)</span><br><span class="line">  object_columns = raw_data.select_dtypes(include=[<span class="string">'object'</span>]).columns</span><br><span class="line">  num_columns = raw_data.select_dtypes(include=[np.number])</span><br><span class="line"></span><br><span class="line">  <span class="comment"># number MinMaxScaler</span></span><br><span class="line">  scaler = MinMaxScaler()</span><br><span class="line">  raw_data[num_columns] = scaler.fit_transform(raw_data[nnum_columns])</span><br><span class="line"></span><br><span class="line">  <span class="comment"># preprocessing categorical</span></span><br><span class="line">  <span class="keyword">for</span> name <span class="keyword">in</span> object_columns:</span><br><span class="line">    raw_data[name] = pd.categorical(raw_data[name]).codes</span><br><span class="line">  <span class="comment"># 训练数据输出预测结果</span></span><br><span class="line">  model = train_model(raw_data)</span><br><span class="line"></span><br><span class="line">  <span class="comment"># kaggle predict fingding donors   test datasets</span></span><br><span class="line"></span><br><span class="line">  test_data = pd.read_csv(<span class="string">'test.csv'</span>)</span><br><span class="line">  <span class="keyword">for</span> name <span class="keyword">in</span> test_data.columns:</span><br><span class="line">    test_data[<span class="string">'name'</span>] = pd.categorical(test_data[name]).codes</span><br><span class="line">  y_test_pred = model.predict(test_data)</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/07/10/finding-donors-1st/" data-id="cjptgn1m7000bev15nd6usbav" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2018/08/21/PseudoBaseStation/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          PseudoBaseStation
        
      </div>
    </a>
  
  
    <a href="/2018/07/10/finding-donors-2nd/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">finding_donors_2nd</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Machine-Learning/">Machine Learning</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linear-regression/">linear regression</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Machine-Learning/" style="font-size: 20px;">Machine Learning</a> <a href="/tags/linear-regression/" style="font-size: 10px;">linear regression</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">December 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/08/">August 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">July 2018</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2018/12/18/SGD/">SGD</a>
          </li>
        
          <li>
            <a href="/2018/12/18/gradient-descent/">gradient_descent</a>
          </li>
        
          <li>
            <a href="/2018/12/06/linear-regression-gluon/">linear-regression-gluon</a>
          </li>
        
          <li>
            <a href="/2018/12/06/linear-regression-sources-md/">linear-regression-sources.md</a>
          </li>
        
          <li>
            <a href="/2018/12/05/regression/">regression</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2018 alex Chen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>